package com.freemarker.product;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.commons.lang3.time.DateUtils;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.format.datetime.joda.DateTimeParser;
import org.springframework.stereotype.Component;

import freemarker.core.Environment;
import freemarker.template.SimpleNumber;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;

@Component
public class TimestampLabel implements TemplateDirectiveModel{

	@Override
	public void execute(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		// 检查是否传递参数，此指令禁止传参！ 
//		if(!params.isEmpty()){
//			throw new TemplateModelException(
//					"This directive do not allow parameters");
//		}
		
		SimpleNumber timestamp = (SimpleNumber) params.get("val");
		String s = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			s = sdf.format(new Date());
//			Date d = sdf.parse(timestamp.toString());
//			s = d.toLocaleString();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		env.getOut().write(s);
	}

}
